package tgc.edu.exam.web.controller;

import java.util.HashSet;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import tgc.edu.exam.custom.AjaxResult;
import tgc.edu.exam.custom.CommonController;
import tgc.edu.exam.custom.dict.entity.SystemData;
import tgc.edu.exam.custom.dict.entity.SystemDictionaries;
import tgc.edu.exam.custom.dict.service.SystemDataService;
import tgc.edu.exam.custom.dict.service.SystemDicationariesService;
import tgc.edu.exam.entity.Committee;
import tgc.edu.exam.entity.Student;
import tgc.edu.exam.entity.SysRole;
import tgc.edu.exam.entity.SysUser;
import tgc.edu.exam.entity.TbClass;
import tgc.edu.exam.security.UserUtils;
import tgc.edu.exam.service.CommitteeService;
import tgc.edu.exam.service.StudentService;
import tgc.edu.exam.service.TbClassService;
import tgc.edu.exam.web.form.CommitteeForm;

@Controller
@RequestMapping(value = "/committee")
public class CommitteeController extends CommonController<Committee, UUID, CommitteeForm> {
	@Autowired
	private SystemDicationariesService dictService;
	@Autowired
	private SystemDataService dataService;
	@Autowired
	private TbClassService tbClassService;
	@Autowired
	private StudentService stuService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private CommitteeService service;

	private Specification<TbClass> buildSpec2(SysUser sysUser) {
		Specification<TbClass> spec = new Specification<TbClass>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<TbClass> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				if (sysUser != null) {
					rules.add(cb.equal(root.get("teacherId").get("id"), sysUser.getId()));
				}
				rules.add(cb.equal(root.get("classStatus").get("state"), "开班"));
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

	@RequestMapping(value = "/manage1")
	public void manage1(ModelMap map,CommitteeForm form) {
		Specification<TbClass> spec = buildSpec2(null);
		List<TbClass> bjs = tbClassService.findAll(spec);
		map.put("bjids", form.getBjid());
		map.put("bjs", bjs);
	}

	@RequestMapping(value = "/edit1")
	public void edit1(CommitteeForm form, ModelMap map) throws InstantiationException, IllegalAccessException {
		String bjid = form.getBjid();
		List<Student> list = stuService.findByTbClassId(UUID.fromString(bjid));
		TbClass tbClass = tbClassService.findById(UUID.fromString(bjid));
		SystemDictionaries systemDictionaries = dictService.findByCode("Class_duties");
		List<SystemData> data = dataService.findBySystemDictionaries(systemDictionaries);
		Committee model= new Committee();
		String id = form.getStuid();
		if(StringUtils.hasText(id)) {
			model=service.findById(UUID.fromString(id));
		}
		map.put("bjs", tbClass);
		map.put("stu", list);
		map.put("data", data);
		map.put("model", model);
	}

	/*@RequestMapping(value = "/select")
	@ResponseBody
	public void select(String stuid,String bjid,String position) {
		Specification<Committee> spec = buildSpec3(bjid, position);
		List<Committee> list = service.findAll(spec);
		Committee committee = new Committee();
		if(list.size()==0) {
			Student student = stuService.findById(UUID.fromString(stuid));
			TbClass tbClass = tbClassService.findById(UUID.fromString(bjid));
			committee.setPosition(position);
			committee.setStudent(student);
			committee.setTbClass(tbClass.getName());
			committee.setTbClassId(tbClass.getId());
			service.save(committee);
		}else {
			
		}
		
	}*/
	
	
	
	private Specification<Committee> buildSpec3(String bjid,String position) {
		Specification<Committee> spec = new Specification<Committee>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<Committee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
					rules.add(cb.equal(root.get("tbClassId"), UUID.fromString(bjid)));
					rules.add(cb.equal(root.get("position"), position));
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

	@Override
	public Specification<Committee> buildSpec(CommitteeForm form) {
		Specification<Committee> spec = new Specification<Committee>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<Committee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				HashSet<Predicate> rules = new HashSet<>();
				String bjid = form.getBjid();
				if(StringUtils.hasText(bjid)) {
					rules.add(cb.equal(root.get("tbClassId"), UUID.fromString(bjid)));
				}
				return cb.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		return spec;
	}

	@RequestMapping(value = "/save1")
	@ResponseBody
	public Object save1(CommitteeForm form) {
		String msg ="";
		try {
			UUID id = form.getId();
			String stuid = form.getStuid();
			String position = form.getPosition();
			Student student = stuService.findById(UUID.fromString(stuid));
			Committee committee;
			if(id!=null) {
				committee = service.findById(id);
			}else {
				committee = new Committee();
			}
				String bjid = form.getBjid();
				TbClass tbClass = tbClassService.findById(UUID.fromString(bjid));
				if(student!=null&&tbClass!=null) {
					committee.setTbClassId(tbClass.getId());
					committee.setTbClass(tbClass.getName());
					committee.setStudent(student);
					if(StringUtils.hasText(position)) {
						Specification<Committee> spec = buildSpec3(bjid, position);
						List<Committee> list = service.findAll(spec);
						if(list.size()==0) {
							committee.setPosition(position);
							service.save(committee);
							msg = "操作成功";
						}else {
							msg = position+"已有人选";
						}
					}else {
						msg = "请选择正确职务";
					}
				}else {
					msg = "数据异常！";
				}
			
		} catch (Exception e) {
			msg = "操作失败";
		}
		
		return msg;
	}

	
	@Override
	public Object delete(CommitteeForm form) {
		try {
			service.deleteById(form.getId());
			return "删除成功";
		} catch (Exception e) {
			return "删除失败";
		}
	}

	

}
